import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {DialogService} from "../../../service/dialogService";
import {UIRiskCheckCriteria} from "../../../entity/UIRiskCheckCriteria";
import {UIRiskCheckPlan} from "../../../entity/UIRiskCheckPlan";
import {CCRiskControllerService} from "../../../service/CCRiskController";
import {WeUIFile, WeUINavBarItem, WeUIUploader} from "angular4-weui";
import {Observable, Subject} from "rxjs";
import {FileService} from "../../../service/fileService";
import {BaseAuthenticateComponent} from "../../../components/baseAuthenticateComponent";
import {isSuccess} from "../../../shared/utils/RestRespTool";
import {debounceTime, distinctUntilChanged} from "rxjs/operators";

@Component({
  selector: 'app-risk-check',
  templateUrl: './risk-check.component.html',
  styleUrls: ['./risk-check.component.css']
})
export class RiskCheckComponent extends BaseAuthenticateComponent implements OnInit {

  searchStream = new Subject<string>();

  criteria: UIRiskCheckCriteria = new UIRiskCheckCriteria();
  valuesForRiskCheckList: UIRiskCheckPlan[] = [];
  riskCheckStatusList = [];
  riskCheckTargetList = [];

  skipFlag = 1;
  loadMore = false;
  remindTxt = '数据已加载完';
  totalRecord: number;
  checkTarget: any;
  status: string;

  items: Observable<string[]>;
  value: string;

  static activeTab = '1';
  activeValue: string;

  constructor(protected a_router: Router, public element: ElementRef, public fileService: FileService,
              public route: ActivatedRoute, public _dialogService: DialogService, public riskService: CCRiskControllerService) {
      super(a_router, _dialogService)
  }

  ngOnInit() {
    this.criteria.pagesize = 10;
    this.criteria.skip = 0;
    this.activeValue = RiskCheckComponent.activeTab;
    // this.status = '';
    // this.checkTarget = '';
    // this.getRiskCheckStatusList();
    // this.getRiskCheckTargetList();
    // this.searchStream
    //   .pipe(
    //     debounceTime(300),     // wait for 500ms pause in events
    //     distinctUntilChanged() // ignore if next search term is same as previous
    //   )
    //   .subscribe((term: string) => {
    //     this.criteria.status = term;
    //     this.refreshRiskCheckList();
    //   });
    this.getRiskCheckByStatus();
  }

  getRiskCheckStatusList() {
    this.riskService.riskCheckStatusList().subscribe((res) => {
      this.riskCheckStatusList = res;
    });
  }

  getRiskCheckTargetList() {
    this.riskService.checkTargetList().subscribe((res) => {
      this.riskCheckTargetList = res;
    });
  }

  refreshRiskCheckList() {
    this.riskService.riskCheckListByCriteria(this.criteria).subscribe(
      (res) => {
        this.valuesForRiskCheckList = res.result;
        this.totalRecord = res.totalRecord;

        this.loadMore = this.totalRecord > this.valuesForRiskCheckList.length;

        if (!this.valuesForRiskCheckList || this.valuesForRiskCheckList.length === 0) {
          this.remindTxt = '暂无数据';
        } else {
          this.remindTxt = this.valuesForRiskCheckList.length + " / " + this.totalRecord;
        }

      }
    );
  }

  refreshSearch() {
    this.criteria.page++;
    this.criteria.status = this.status;
    this.criteria.checkTarget = this.checkTarget;

    if (this.status === '') {
      delete this.criteria.status;
    }
    if (this.checkTarget === '') {
      delete this.criteria.checkTarget;
    }
    this.refreshRiskCheckList();
  }

  onScroll() {
    this.criteria.skip = this.criteria.pagesize * this.skipFlag;
    console.log(JSON.stringify(' totalRecord =======> ' + this.totalRecord));
    this.riskService.riskCheckListByCriteria(this.criteria).subscribe(
      res => {
        this.valuesForRiskCheckList = this.valuesForRiskCheckList.concat(res.result)
        if (this.totalRecord > this.valuesForRiskCheckList.length) {
          this.loadMore = true;
          this.skipFlag++;
        } else {
          this.loadMore = false;
        }

        this.remindTxt = this.valuesForRiskCheckList.length + " / " + this.totalRecord;
      }
    );
  }

  search(value: string): void {
    this.searchStream.next(value);
  }

  detail(event: UIRiskCheckPlan) {
    this.a_router.navigate(["/dashboard/plan", event.id, "detail"]);
  }

  onActivated(item: WeUINavBarItem): void {
    // 恢复滚动条的垂直位置,从头开始
    this.skipFlag = 1;
    this.criteria.skip = 0;
    this.loadMore = null;
    this.activeValue = item.value;
    RiskCheckComponent.activeTab = this.activeValue;
    this.getRiskCheckByStatus();
  }

  getRiskCheckByStatus() {
    switch (this.activeValue) {
      case '1':
        this.criteria.status = 'checking';
        this.refreshRiskCheckList();
        break;
      case '2':
        this.criteria.status = 'finished';
        this.refreshRiskCheckList();
        break;
    }
  }

  getTabActivated() {
    return RiskCheckComponent.activeTab;
  }

}
